package com.chat.view;

import com.chat.client.ClientChat;
import javafx.scene.control.Label;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.*;
import java.util.Timer;

public class ClientView extends JFrame{
    //聊天框 文本区域
    private static JTextArea liaotianquyu = new JTextArea();
    private static JList<String> list = new JList<>();
    private static String mas = "";
    ClientChat client = null;

    public ClientView(ClientChat client){
        this.client = client;
    }

    public void init(){
        //设置窗口标题
        this.setTitle("聊天客户端");
        //设置窗口大小和初始位置
        this.setBounds(700,200,600,600);
        //设置窗口不可改变
        this.setResizable(false);

        //创建面板
        JPanel panel = new JPanel();
        //设置布局模式为绝对布局
        panel.setLayout(null);

        //状态标签
        JLabel Label = new JLabel();
        Label.setBounds(10,5,420,30);
        Label.setText("id：" + client.getId() + " 用户名：" + client.getUser() + " 服务器地址：" + client.getSip() +":"+client.getSport());
        panel.add(Label);

        //在线用户
        JLabel ZLabel = new JLabel();
        ZLabel.setBounds(440,5,150,30);
        ZLabel.setText("当前在线用户" );
        panel.add(ZLabel);

        list.setBounds(440,35,150,520);
        panel.add(list);

        //聊天框 文本区域
        liaotianquyu.setBounds(10,35,420,350);
        //设置自动换行
        liaotianquyu.setLineWrap(true);
        //设置不可编辑
        liaotianquyu.setEnabled(false);
        //给聊天框添加滚动条
        JScrollPane sp1 = new JScrollPane();
        sp1.setBounds(10,35,420,350);
        sp1.setViewportView(liaotianquyu);
        //最终将 sp 添加到面板中
        panel.add(sp1);

        //信息输入框 文本区域
        JTextArea shuru = new JTextArea();
        shuru.setBounds(10,400,420,120);
        shuru.setLineWrap(true);
        JScrollPane sp2 = new JScrollPane();
        sp2.setBounds(10,400,420,120);
        sp2.setViewportView(shuru);

        shuru.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e){
                //当按下Enter键时，插入一个换行符(Enter键的keycode为10)
                if(e.getKeyCode()==10){
                    try {
                        String text = shuru.getText();
                        //清除换行
                        text = text.replaceAll("\r|\n", "");
                        inmas(text);

                    } catch (IOException | ClassNotFoundException ioException) {
                        ioException.printStackTrace();
                    }
//                    liaotianquyu.append(shuru.getText()+"\n");
                    shuru.setText("");
                }
            }

        });
        panel.add(sp2);

        //创建发送信息按钮
        JButton fasong = new JButton("发送");
        fasong.setBounds(10,525,420,30);
        //按钮事件
        fasong.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    String text = shuru.getText();
                    //清除换行
                    text = text.replaceAll("\r|\n", "");
                    inmas(text);

                } catch (IOException | ClassNotFoundException ioException) {
                    ioException.printStackTrace();
                }
//                    liaotianquyu.append(shuru.getText()+"\n");
                shuru.setText("");
            }
        });
        panel.add(fasong);

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {

                try {
                    client.putNews("exit","");
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                } catch (ClassNotFoundException classNotFoundException) {
                    classNotFoundException.printStackTrace();
                }

                System.exit(0);
            }
        });


        //将面板加入窗口
        this.add(panel);
        //设置窗口可见
        this.setVisible(true);

    }

    public void start(){
        this.init();

        //获取消息
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                try {

                    inmas("");
                    System.out.println("1");
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }

        },0,1500);

        //获取用户列表
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                try {

                    String[] words = client.putNews("list", "").split("\n");

                    list.setListData(words);

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }

        },0,10*1000);
    }


    public void inmas(String data) throws IOException, ClassNotFoundException {

        String mas = client.putNews("put",data);
        System.out.println(mas);
        liaotianquyu.setText(mas);

    }

}
